#!/usr/bin/python

import json
import sys
import numpy as np


validLabels = None;
cnt = 0;

def main():

	global validLabels;
	global cnt;
	validLabels = np.loadtxt('../validLabels.txt',dtype=int);
	with open('../../../Data229/amazon_products') as f:
		for line in f:

			cnt+=1

			line = line.strip('\x01\n')
			j = json.loads(line)

			#print json.dumps(j['Item'],indent=4)
			#Get label

			#printItemLabel(j);
			#print json.dumps(j['Item']['ItemAttributes'],indent=4)
			#print json.dumps(j['Item']['BrowseNodes'],indent=4)
			#print ''
			#print j['Item']['ItemAttributes']['Title']
			#print j['Item']['DetailPageURL'];
			#print 'ProductGroup: ', j['Item']['ItemAttributes']['ProductGroup'];
			#print 'Department: ', j['Item']['ItemAttributes']['Department'];
			#print 'ProductTypeName: ', j['Item']['ItemAttributes']['ProductTypeName'];
			#print ''
			#print '--------------------------------'
			#print ''
			if cnt == 50:
				print json.dumps(j['Item'],indent=4)
				raw_input('Enter...');
		#for i in j:

			
			#if 'Children' in i:
			#	print i['Name'],':',len(i['Children']);
			#else:
			#	print i['Name']
	print 'Total Items: ',cnt
			
	#print cnt

def printItemLabel(n):
	#No Nodes
	if 'BrowseNodes' not in n['Item']:
		print 0
		#print json.dumps(n['Item'],indent=4)
	#Single Nodes
	elif 'Ancestors' in n['Item']['BrowseNodes']['BrowseNode']:
		printBrowseNode(n['Item']['BrowseNodes']['BrowseNode']);
	#Array of nodes
	else:
		for i in n['Item']['BrowseNodes']['BrowseNode']:
			printBrowseNode(i);

def printBrowseNode(n):	
	if 'Ancestors' in n:
		printBrowseNode(n['Ancestors']['BrowseNode']);
	elif int(n['BrowseNodeId']) in validLabels:
		print int(n['BrowseNodeId'])

def getLabel(n):

	#print '----------'
	#print json.dumps(n,indent=4)
	
	if int(n['BrowseNodeId']) in validLabels:
		return int(n['BrowseNodeId'])
	else:
		if 'Ancestors' in n:
			return getLabel(n['Ancestors']['BrowseNode'])
		else:
			return -1

if __name__ == "__main__":
    main()
